#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   ：js逆向 -> 异步任务的返回值
@IDE    ：PyCharm
@Author ：王启俊
@Date   ：2024-03-30 16:05
@Desc   ：
=================================================='''


import asyncio
import time

async def foo(i):
    print(f'任务{i} start')
    await asyncio.sleep(i)
    print(f'任务{i}  end')
    return i*i






# 创建协程对象
task1 = [
    asyncio.ensure_future(foo(1)),
    asyncio.ensure_future(foo(2)),
    asyncio.ensure_future(foo(3)),
]

# 添加回调 如果有返回值了
def fn(obj):
    print("任务1结束啦", obj,obj.result)
task1[0].add_done_callback(fn)

start = time.time()
# 使用gather()方法运行协程任务
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(task1))

for i in task1:
    print(i.done(), i.result())

print(start-time.time())

